;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.SaleCompare", [])
        .controller("SaleCompareCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false, false, false, false, false, false, false];

            $scope.condition = {reportType: 1};
            $scope.dataList = [];
            $scope.clientClass = [];

            $scope.switchDatePicker = function (index) {
                $scope.datePickerFlag = [false, false, false, false, false, false, false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        type: "",
                        name: "零件分类",
                        dataField: "materialsType",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已出库金额-时间1",
                        dataField: "outPrice1",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "已出库金额-时间2",
                        dataField: "outPrice2",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已出库金额-时间3",
                        dataField: "outPrice3",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已出库金额-时间4",
                        dataField: "outPrice4",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已开票金额-时间1",
                        dataField: "ticketPrice1",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已开票金额-时间2",
                        dataField: "ticketPrice2",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已开票金额-时间3",
                        dataField: "ticketPrice3",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已开票金额-时间4",
                        dataField: "ticketPrice4",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订单金额-时间1",
                        dataField: "orderPrice1",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订单金额-时间2",
                        dataField: "orderPrice2",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订单金额-时间3",
                        dataField: "orderPrice3",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订单金额-时间4",
                        dataField: "orderPrice4",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.sapStatusEnum = [
                {key: 2, value: "已过账"},
                {key: 1, value: "部分过账"},
                {key: 0, value: "未过账"}
            ];


            $scope.yesOrNo = [
                {key: 1, value: "是"},
                {key: 0, value: "否"}
            ];

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/report/exportSaleCompareList")
            };

            $scope.reset = function () {
                $scope.condition = {reportType: 1};
            };

            var btnPermissionCode = {
                export: window.ActionCode.threeReportExport
            };

            $scope.btnPermission = {
                export: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function checkHasValue(value1, value2) {
                if (!!value1 && !!value2) {
                    return true;
                } else {
                    return false;
                }
            }

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var dataFlag = false;
                for (var i = 1; i <= 4; i++) {
                    if (!!$scope.condition['startDate' + i] || !!$scope.condition['endDate' + i]) {
                        dataFlag = checkHasValue($scope.condition['startDate' + i], $scope.condition['endDate' + i])
                    }
                    if (!dataFlag) {
                        ybWidget.ShowMessage("请在高级搜索中填写完整时间段", MESG_TYPE.Error).then(function () {
                            window.event.stopPropagation();
                        });
                        return;
                    }
                }


                var service = "device";
                var reqUrl = "/report/saleCompareList";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;

                    angular.forEach($scope.dataList, function (item) {
                        item.createDateStr = $filter('YbDate')(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    });
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getClientClass() {
                var service = "device";
                var reqUrl = "/dealerClass/list";
                var param = {};
                param.pId = 0;
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code != 0) {
                        ybWidget.ShowMessage(resp);
                        return;
                    }
                    $scope.clientClass = resp.value;
                    return resp.value;
                })
            };

            (function () {
                //getDataList();
                getClientClass();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);